<?php
class SiteModel extends Model {
	private $pagesDB;
	
	public function __construct(){
		parent::__construct();
		$this->pagesDB = D('Pages');
	}
	protected $_validate = array(
	    array('title','require','请填写网站名称！'),
		
    );
	protected $_auto = array(
        array('create_time','time',1,'function'),
		array('update_time','time',2,'function'),
		array('update_time','time',1,'function'),
    ); 
	//获取当前站点的管理用户ID，返回ID自然数组
	public function getUser_id($site_id){
		$db = M('Site_user');
		$data = $db -> where(array('site_id' => $site_id)) -> Field('user_id') -> select();
		$ret = array();
		foreach($data as $v){
			$ret[] = $v['user_id'];
		}
		//dump($ret);
		return $ret;
	}
	//检查用户是否有管理权限
	//$uid 用户ID
	//$sid 站点ID
	public function getManage($uid,$sid){
		if(in_array($uid,$this->getUser_id($sid))){
			return true;
		}else{
			return false;
		}
	}
	//当前站点的所有用户
	public function getUser_name($site_id){
		$ids = $this->getUser_id($site_id);
		$map['id'] = array('in', $ids); 
		$data = M('User') -> where($map) ->Field('id,nickname') -> select();
		return $data;
	}
	
	//当前用户的所有站点
	public function getSite_list($user_id){
		$db = M('Site_user');
		$data = $db -> alias('su')->join("inner join __SITE__ as s on s.id = su.site_id")-> where(array('user_id' => $user_id, 's.id' => array('gt', 0))) -> Field('s.*') -> select();
		
		return $data;
	}
	
	//一键复制
	public function copy(){
		ob_start();
		set_time_limit(0);
		if(isset($_POST['id'])){
			$id = intval($_POST['id']);
			if($id == 0) return false;
			$sdata = $this->where(array('id' => $id))->find();
			if(!$sdata){
				$this->error('参数错误!');
			}else{
				unset($sdata['id']);
				$sdata['price'] = '';
				$sdata['create_time'] = time();
				$sdata['timeout'] = time() + (isset($_POST['timeout'])? $_POST['timeout'] * 31 * 24 * 3600 : 31 * 24 * 3600);
				$sdata['title'] = date('Y-m-d H:i:s').'-新站点';
				
				$sdata['status'] = 0;
				
				if($_POST['title']){
					//使用用户提交上来的名称来命名
					$sdata['title'] = $_POST['title'];
				}
				$sdata['domain'] = '';
				$sdata['uid'] = 0;
				if($sdata['synchronous'] == 1){
					$sdata['relate_id'] = $id; 
				}
				//$sdata['relate_id'] = $id;
				$insert_id = $this -> data($sdata) -> add();
				//复制表单，在模块复制时复制
				//D('Form') -> copy($id, $insert_id, array('site_id' => $id));
				//复制页面
				if(in_array('Pages', $_POST['copydata'])){
					$mdb = D('Pages');
				    $mdb -> copy($id, $insert_id);
				}
				//复制积分管理系统
				if(in_array('Intesys', $_POST['copydata'])){
					//复制表格分类（同时复制表格），表格中有积分规则，下一步需要复制积分规则，以替换表格中的规则ID
					D('Formcategory') -> copy($id, $insert_id, array('site_id' => $id, 'pid' => 0));
					//复制规则分类，同时复制规则，规则复制过程中，表格的规则ID集合将被替换成新的ID
					D('Integral_ruletype') -> copy($id, $insert_id, array('site_id' => $id, 'pid' => 0));
					//复制用户组，同时授权
					D('Member_group') -> copy($id, $insert_id, array('site_id' => $id, 'pid' => 0));
					//复制积分类型
					D('Integral_type') -> copy($id, $insert_id, array('site_id' => $id));
					//复制结束
					
					
					/*错误写法，缺少参数
					$gdb = D('Member_group');
				    $gdb -> copy($id, $insert_id);
                    $rdb = D('Integral_ruletype');
				    $rdb -> copy($id, $insert_id);
                    $tdb = D('Integral_type');
				    $tdb -> copy($id, $insert_id);*/
				}
				//设置站点风格
				
				$currentSite = $this->where(array('id' => $insert_id)) -> find();
				
				$saveData['page_header_id'] = $this->pagesDB -> where(array('site_id' =>$insert_id, 'rid' => $currentSite['page_header_id'])) -> getField('id');
				$saveData['page_id'] = $this->pagesDB -> where(array('site_id' =>$insert_id, 'rid' => $currentSite['page_id'])) -> getField('id');
				$saveData['page_footer_id'] = $this->pagesDB -> where(array('site_id' =>$insert_id, 'rid' => $currentSite['page_footer_id'])) -> getField('id');
				
				$this -> where(array('id' => $insert_id)) -> data($saveData) -> save();
				
				
				//复制系统页面
				D('Systempages') -> copy($id, $insert_id, array('site_id' => $id));
				
				
				//复制栏目和数据
				
				
				$map['pid'] = 0;
				$map['site_id'] = $id;
				foreach($_POST['copydata'] as $table){
					if($table == 'Product_category'){
						$cdb = D('Product_category');
						$cdb -> copy($id, $insert_id, $map, 0);
					}
					if($table == 'Category'){
						$cdb = D('Category');
						$cdb -> copy($id, $insert_id, $map, 0);
					}
			    }
					//更新该模块中的数据
				D('Modules') -> _flush($insert_id);
			}
			
			
			//$this->where(array('id' => $insert_id)) -> data(array('status' => 1)) -> save();
			
			return $insert_id;
			
		}
	}
	
	
	//积分系统检测当前站点员工是否超过允许用户数
	public function judgeMaxNum(){
		$maxNum=$this->where(['id'=>__SITEID__])->getField('maxNum');
		$nowNum=M('Member')->where(['site_id'=>__SITEID__,'isadministrator'=>0])->count('id');
		if($maxNum<=$nowNum){
			return false;
		}else{
			return $nowNum;	
		}
	}
	
	
}